<template>
    <div>
        <h1 style="text-align:center;">Training Activity Entry</h1>
        <el-form :inline="true" style="text-align:center;">
            <el-form-item>
              <el-input v-model="trainingName" placeholder="Please enter the training name"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="success" @click="queryPageData">search</el-button>
              <el-button type="primary" @click="addTrainInfo">Enter Training Activity</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="tableData" border style="width: 100%"> 
            <el-table-column prop="trainingName" label="trainingName" align="center"></el-table-column>
            <el-table-column prop="trainingLevel" label="trainingLevel" align="center"></el-table-column>
            <el-table-column prop="trainingAddress" label="trainingAddress" align="center"></el-table-column>
            <el-table-column prop="trainingStartTime" label="trainingStartTime" align="center"></el-table-column>
            <el-table-column prop="trainingEndTime" label="trainingEndTime" align="center"></el-table-column>
            <el-table-column prop="comment" label="comment" align="center"></el-table-column>
            <el-table-column label="operation" align="center">
                <template slot-scope="scope">
                    <el-button type="primary" icon="el-icon-edit" circle @click="updateTrainInfo(scope.row.id)"></el-button>
                    <el-button type="danger" icon="el-icon-delete" circle @click="deleteTrainInfo(scope.row.id)"></el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
        <addTrainInfoDialog v-if="addTrainInfoDialogVisible" ref="addTrainInfoDialog" @refreshDataList="queryPageData"></addTrainInfoDialog>
    </div>
</template>

<script>
  import addTrainInfoDialog from './addTrainInfoDialog'
  export default {
    data () {
      return {
        tableData: [
          {
            trainingName: '',
            trainingLevel: '',
            trainingAddress: '',
            trainingStartTime: '',
            trainingEndTime: '',
            comment: ''
          }
        ],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        trainingName: '',
        addTrainInfoDialogVisible: false
      }
    },
    components: {
      addTrainInfoDialog
    },
    activated () {
      this.queryPageData()
    },
    methods: {
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.queryPageData()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.queryPageData()
      },
      // 培训管理信息分页
      queryPageData () {
        this.$http({
          url: this.$http.adornUrl('/edu/tedutrain/queryPageData'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'trainingName': this.trainingName
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.tableData = data.data.list
            this.totalPage = data.data.total
          } else {
            this.tableData = []
            this.totalPage = 0
          }
        })
      },
      // 录入培训管理信息
      addTrainInfo () {
        this.addTrainInfoDialogVisible = true
        this.$nextTick(() => {
          this.$refs.addTrainInfoDialog.init()
        })
      },
      // 修改培训管理信息
      updateTrainInfo (id) {
        this.addTrainInfoDialogVisible = true
        this.$nextTick(() => {
          this.$refs.addTrainInfoDialog.init(id)
        })
      },
      // 删除培训管理信息
      deleteTrainInfo (id) {
        this.$http({
          url: this.$http.adornUrl(`/edu/tedutrain/` + id),
          method: 'delete'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message.success('Deletion succeeded!')
            this.queryPageData()
          } else {
            this.$message.error(data.msg)
          }
        })
      }
    }
  }
</script>